import { Pipe, PipeTransform } from '@angular/core';
// 自定义时间
@Pipe({
    name: 'customDateFilter',
})
export class CustomDateFilter implements PipeTransform {
    transform(value: any, type?: any): any {
        let  result = '';
        if (type !== undefined) {
            // console.log(new Date().toLocaleString() + '.' + new Date().getMilliseconds() + '  	   ', type);
            const date = new Date(value);
            result = this.formatDate(date, "yyyy-MM-dd hh:mm");
        } else {
            const date = new Date((value - 0) * 1000);
            result = this.formatDate(date, "yyyy-MM-dd hh:mm");
        }
        return result;
    }

    formatDate = (dateToFormat, format) => {
        const date = {
            "M+": dateToFormat.getMonth() + 1,
            "d+": dateToFormat.getDate(),
            "h+": dateToFormat.getHours(),
            "m+": dateToFormat.getMinutes(),
            "s+": dateToFormat.getSeconds(),
            "q+": Math.floor((dateToFormat.getMonth() + 3) / 3),
            "S+": dateToFormat.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (dateToFormat.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (const k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }


    isValidDate = (prettyDate) => {
        return prettyDate instanceof Date && !isNaN(prettyDate.getTime());
    }

}
